﻿import { vb } from "./interface/voidbox.js"

//var logo: vb.Sprite;
var texwhite: vb.Texture;
var texgrid: vb.Texture;
var texbig: vb.Texture;
var spriteLogo: vb.Sprite;

var spriteWord: vb.Sprite;

var sound: vb.Sound;
var bgm: vb.BGM;
function init() {
    vb.Init();

    vb.debug_Log("log", "我胡汉三又回来了");

    var logo = vb.imageData_Create(100, 100, false);
    texwhite = vb.texture_GetWhite();

    var grid = vb.imageData_Create(2, 2, false);
    grid.data[0] = 255;
    grid.data[1] = 255;
    grid.data[2] = 255;
    grid.data[3] = 128;
    grid.data[12] = 255;
    grid.data[13] = 255;
    grid.data[14] = 255;
    grid.data[15] = 128;
    texgrid = vb.texture_CreateFromImageData(grid);


    spriteLogo = vb.sprite_GetLogo();
    vb.debug_Log("log", "spriteLogo:" + JSON.stringify(spriteLogo));

    vb.app_SetClearColor(new vb.Color(0.3, 1, 0.8));

    var buf = vb.app_LoadBinary("back1.png");
    vb.debug_Log("log", "buf.length=" + buf.byteLength);
    var idata = vb.imageData_Decode(buf);
    vb.debug_Log("log", "load texture " + idata.width + "," + idata.height);
    texbig = vb.texture_CreateFromImageData(idata);


    var fontbuf = vb.app_LoadBinary("VonwaonBitmap-16px.ttf");
    var font = vb.font_Create(fontbuf);
    vb.debug_Log("log", "font=" + JSON.stringify(font));
    var ccode = "家".charCodeAt(0);
    vb.debug_Log("log", "ccode=" + ccode);
    spriteWord = font.GetSprite(16, ccode);

    vb.app_SetOnKey(onkey);
    vb.app_SetOnPoint(onpoint);

    var soundbin = vb.app_LoadBinary("sound/attack2_laser.mp3");
    sound = vb.SoundSystem.CreateSound(soundbin);

    var bgmbin = vb.app_LoadBinary("sound/Mars.wav");
    bgm = vb.SoundSystem.CreateBGM(bgmbin);
}
function onkey(keycode: vb.KeyCode, down: boolean) {
    if (keycode == vb.KeyCode.D1) {
        sound.Play();
    }
    if (keycode == vb.KeyCode.D2) {
        vb.SoundSystem.PlayBGM(bgm);
    } if (keycode == vb.KeyCode.D3) {
        vb.SoundSystem.StopBGM();
    }
    vb.debug_Log("log", "keycode:" + keycode + " = " + down)
}
function onpoint(tid: number, press: boolean, x: number, y: number) {
    vb.debug_Log("log", "tid:" + tid + " press=" + press + " x=" + x + " y=" + y);
}
//系统Update 时间
var timer = 0;
function update(delta: number) {
    timer += delta;
    if (timer > 1) {
        console.log("log 只会显示在控制台，这需要用调试器才能看到", "delta=" + delta);
        vb.debug_Log("log", "debug_Log 会显示在界面上 (打开debug选项时) delta=" + delta);
        timer = 0;
    }
    //for (var i = 0; i < 10000; i++) {
    //    vb.debug_Test("update", "update" + delta);
    //}
    //console.log("log","update"+delta);
}
function render() {
    //vb.debug_Log("r", texwhite.handle.toString());
    var color = new vb.Color(0.8, 0.6, 0.8);
    texbig.Draw(0, 0, vb.app_GetPixelWidth(), vb.app_GetPixelHeight());
    texwhite.Draw(0, 0, 100, 100, 0, 0, 1, 1, 0, color);

    texgrid.Draw(110, 0, 160, 50, 0, 0, 1, 1, 0, color);

    for (var y = 0; y < 10; y++) {
        for (var x = 0; x < 10; x++) {
            spriteLogo.Draw(x * 32, y * 32, 0, 0, 1, 1, vb.Color.White);

        }
    }
    for (var y = 0; y < 10; y++) {
        for (var x = 0; x < 10; x++) {

            spriteWord.Draw(x * 48, y * 32, 0, 0, 1, 1, vb.Color.White);
        }
    }


}


vb.app_SetGameUpdate(update);
vb.app_SetGameRender(render);
init();